
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>增删改查 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="table.html" />
    
    
    <link rel="prev" href="param.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速开始
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="param.html">
            
                <a href="param.html">
            
                    
                    参数获取
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.2" data-path="crud.html">
            
                <a href="crud.html">
            
                    
                    增删改查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="table.html">
            
                <a href="table.html">
            
                    
                    单表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="page.html">
            
                <a href="page.html">
            
                    
                    分页查询
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    核心模块
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../module/db.html">
            
                <a href="../module/db.html">
            
                    
                    db模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../module/http.html">
            
                <a href="../module/http.html">
            
                    
                    http模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../module/log.html">
            
                <a href="../module/log.html">
            
                    
                    log模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../module/env.html">
            
                <a href="../module/env.html">
            
                    
                    env模块
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    特殊函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../function/agg.html">
            
                <a href="../function/agg.html">
            
                    
                    聚合函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../function/date.html">
            
                <a href="../function/date.html">
            
                    
                    日期函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../function/string.html">
            
                <a href="../function/string.html">
            
                    
                    字符串函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../function/array.html">
            
                <a href="../function/array.html">
            
                    
                    数组函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../function/math.html">
            
                <a href="../function/math.html">
            
                    
                    数学函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../function/other.html">
            
                <a href="../function/other.html">
            
                    
                    其他函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    对象方法
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../object/object.html">
            
                <a href="../object/object.html">
            
                    
                    Object对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../object/number.html">
            
                <a href="../object/number.html">
            
                    
                    Number对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../object/list.html">
            
                <a href="../object/list.html">
            
                    
                    数组&集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../object/date.html">
            
                <a href="../object/date.html">
            
                    
                    Date对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../object/class.html">
            
                <a href="../object/class.html">
            
                    
                    Class对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../object/pattern.html">
            
                <a href="../object/pattern.html">
            
                    
                    Patter对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    脚本语法
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../syntax/keyword.html">
            
                <a href="../syntax/keyword.html">
            
                    
                    关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../syntax/type.html">
            
                <a href="../syntax/type.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../syntax/yunsuan.html">
            
                <a href="../syntax/yunsuan.html">
            
                    
                    运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../syntax/yiyuan.html">
            
                <a href="../syntax/yiyuan.html">
            
                    
                    运算符示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../syntax/for.html">
            
                <a href="../syntax/for.html">
            
                    
                    for循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../syntax/import.html">
            
                <a href="../syntax/import.html">
            
                    
                    导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../syntax/async.html">
            
                <a href="../syntax/async.html">
            
                    
                    异步
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >增删改查</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="sql&#x53C2;&#x6570;">SQL&#x53C2;&#x6570;</h3>
<h4 id="&#x6CE8;&#x5165;&#x53C2;&#x6570;">#{} &#x6CE8;&#x5165;&#x53C2;&#x6570;</h4>
<p>&#x4F5C;&#x7528;&#x548C;<code>mybatis</code>&#x4E00;&#x81F4;&#xFF0C;&#x90FD;&#x662F;&#x5C06;<code>#{}</code>&#x533A;&#x57DF;&#x66FF;&#x6362;&#x4E3A;&#x5360;&#x4F4D;&#x7B26;<code>?</code></p>
<pre><code class="lang-groovy">var id = <span class="hljs-number">123</span>;
<span class="hljs-keyword">return</span> db.select(<span class="hljs-string">&quot;&quot;&quot;
    select * from sys_user where id = #{id}
&quot;&quot;&quot;</span>);
</code></pre>
<p>&#x8FD0;&#x884C;&#x65F6;&#x751F;&#x6210;&#x7684;SQL&#x4E3A;&#xFF1A;<code>select * from sys_user where id = ?</code>&#x3002;</p>
<p>&#x53C2;&#x6570;<code>id</code>&#x503C;&#x4F1A;&#x88AB;&#x6CE8;&#x5165;&#x4E3A;<code>123</code>&#x3002;</p>
<p>&#x6B64;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x907F;&#x514D;<code>sql</code>&#x6CE8;&#x5165;&#x3002;</p>
<h4 id="&#x62FC;&#x63A5;&#x53C2;&#x6570;">${} &#x62FC;&#x63A5;&#x53C2;&#x6570;</h4>
<p>&#x4F5C;&#x7528;&#x548C;<code>mybatis</code>&#x4E00;&#x81F4;&#xFF0C;&#x90FD;&#x662F;&#x5C06;<code>${}</code>&#x533A;&#x57DF;&#x66FF;&#x6362;&#x4E3A;&#x5BF9;&#x5E94;&#x7684;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code class="lang-groovy">var id = <span class="hljs-number">123</span>;
<span class="hljs-keyword">return</span> db.select(<span class="hljs-string">&quot;&quot;&quot;
    select * from sys_user where id = ${id}
&quot;&quot;&quot;</span>);
</code></pre>
<p>&#x8FD0;&#x884C;&#x65F6;&#x751F;&#x6210;&#x7684;SQL&#x4E3A;&#xFF1A;<code>select * from sys_user where id = 123</code></p>
<h4 id="&#x52A8;&#x6001;sql&#x53C2;&#x6570;">&#x52A8;&#x6001;SQL&#x53C2;&#x6570;</h4>
<p>&#x901A;&#x8FC7;<code>?{condition,expression}</code>&#x6765;&#x5B9E;&#x73B0;&#x52A8;&#x6001;&#x62FC;&#x63A5;<code>SQL</code>&#xFF0C;&#x5982;&#x679C;&#x6761;&#x4EF6;&#x6210;&#x7ACB;&#x5219;&#x62FC;&#x63A5;&#x540E;&#x90E8;&#x5206;&#x5185;&#x5BB9;<code>SQL</code>&#x4E2D;&#xFF0C;&#x4E0E;<code>mybatis</code>&#x4E2D;&#x7684;<code>if</code>&#x6807;&#x7B7E;&#x57FA;&#x672C;&#x4E00;&#x81F4;</p>
<pre><code class="lang-groovy"><span class="hljs-keyword">return</span> db.select(<span class="hljs-string">&quot;select * from sys_user ?{id,where id = #{id}}&quot;</span>);
<span class="hljs-comment">// &#x5F53;id&#x6709;&#x503C;&#x65F6;,&#x751F;&#x6210;SQL&#xFF1A;select * from sys_user where id = ?`&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;mybatis&#x4E2D;&#x7684;&lt;if test=&quot;id != null and id != &apos;&apos;&quot;&gt;</span>
<span class="hljs-comment">// &#x5F53;id&#x65E0;&#x503C;&#x65F6;,&#x751F;&#x6210;SQL&#xFF1A;select * from sys_user</span>
<span class="hljs-keyword">return</span> db.select(<span class="hljs-string">&quot;select * from sys_user ?{id!=null&amp;&amp;id.length() &gt; 3,where id = #{id}}&quot;</span>);
<span class="hljs-comment">// &#x5F53;id!=null&amp;&amp;id.length() &gt; 3&#x5224;&#x65AD;&#x4E3A;true&#x65F6;,&#x751F;&#x6210;SQL&#xFF1A;`select * from sys_user where id = ?</span>
<span class="hljs-comment">// &#x5F53;&#x5224;&#x65AD;&#x4E3A;false&#x65F6;,&#x751F;&#x6210;SQL&#xFF1A;select * from sys_user</span>
</code></pre>
<h4 id="sql&#x7F13;&#x5B58;">SQL&#x7F13;&#x5B58;</h4>
<pre><code class="lang-groovy"><span class="hljs-comment">// &#x5C06;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x7F13;&#x5B58;&#x5230;&#x540D;&#x4E3A;user_cache&#x7684;&#x7F13;&#x5B58;&#x4E2D;&#xFF0C;&#x6709;&#x6548;&#x671F;1&#x5C0F;&#x65F6;</span>
<span class="hljs-keyword">return</span> db.cache(<span class="hljs-string">&quot;user_cache&quot;</span>, <span class="hljs-number">3600</span> * <span class="hljs-number">1000</span>).select(<span class="hljs-string">&quot;&quot;&quot;
    select * from sys_user
&quot;&quot;&quot;</span>)
<span class="hljs-comment">// &#x5F53;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x8BED;&#x53E5;&#x65F6;&#xFF0C;&#x5C06;&#x6E05;&#x7A7A;user_cache&#x7F13;&#x5B58;</span>
db.cache(<span class="hljs-string">&quot;user_cache&quot;</span>).update(<span class="hljs-string">&quot;&quot;&quot; ...... &quot;&quot;&quot;</span>)
db.cache(<span class="hljs-string">&quot;user_cache&quot;</span>).insert(<span class="hljs-string">&quot;&quot;&quot; ...... &quot;&quot;&quot;</span>)
</code></pre>
<h3 id="&#x4F7F;&#x7528;&#x4E8B;&#x52A1;">&#x4F7F;&#x7528;&#x4E8B;&#x52A1;</h3>
<h4 id="&#x81EA;&#x52A8;&#x4E8B;&#x52A1;">&#x81EA;&#x52A8;&#x4E8B;&#x52A1;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> val = db.transaction(()=&gt;{
    <span class="hljs-keyword">var</span> v1 = db.update(<span class="hljs-string">&apos;...&apos;</span>);
    <span class="hljs-keyword">var</span> v2 = db.update(<span class="hljs-string">&apos;....&apos;</span>);
    <span class="hljs-keyword">return</span> v2;
});
<span class="hljs-keyword">return</span> val;
</code></pre>
<h4 id="&#x624B;&#x52A8;&#x4E8B;&#x52A1;">&#x624B;&#x52A8;&#x4E8B;&#x52A1;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> tx = db.transaction();  <span class="hljs-comment">//&#x5F00;&#x542F;&#x4E8B;&#x52A1;</span>
<span class="hljs-keyword">try</span>{
    <span class="hljs-keyword">var</span> value = db.update(<span class="hljs-string">&apos;...&apos;</span>);
    tx.commit();    <span class="hljs-comment">// &#x63D0;&#x4EA4;&#x4E8B;&#x52A1;</span>
    <span class="hljs-keyword">return</span> value;
}<span class="hljs-keyword">catch</span>(e){
    tx.rollback();  <span class="hljs-comment">// &#x56DE;&#x6EDA;&#x4E8B;&#x52A1;</span>
}
</code></pre>
<h3 id="mybatis&#x8BED;&#x6CD5;&#x652F;&#x6301;">Mybatis&#x8BED;&#x6CD5;&#x652F;&#x6301;</h3>
<p>&#x53C2;&#x8003;: <a href="http://www.mybatis.cn/mybatis/index.html" target="_blank">http://www.mybatis.cn/mybatis/index.html</a></p>
<h4 id="&#x5173;&#x952E;&#x5B57;">&#x5173;&#x952E;&#x5B57;</h4>
<p>&#x76EE;&#x524D;&#x652F;&#x6301;&#x7684;&#x5173;&#x952E;&#x5B57;&#x5982;&#x4E0B;</p>
<table>
<thead>
<tr>
<th>&#x5173;&#x952E;&#x5B57;</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&lt;if&gt;</code></td>
</tr>
<tr>
<td><code>&lt;elseif&gt;</code></td>
</tr>
<tr>
<td><code>&lt;else&gt;</code></td>
</tr>
<tr>
<td><code>&lt;where&gt;</code></td>
</tr>
<tr>
<td><code>&lt;foreach&gt;</code></td>
</tr>
<tr>
<td><code>&lt;trim&gt;</code></td>
</tr>
<tr>
<td><code>&lt;set&gt;</code></td>
</tr>
</tbody>
</table>
<h4 id="if">if</h4>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-js">var sql = &quot;&quot;&quot;
select * from test_data
  where 1 = 1
  &lt;if test=&quot;id != null&quot;&gt;
        and id = #{id}
    &lt;/if&gt;
&quot;&quot;&quot;
return db.select(sql)
</code></pre>
<p>&#x8FD9;&#x6761;&#x8BED;&#x53E5;&#x63D0;&#x4F9B;&#x4E86;&#x53EF;&#x9009;&#x7684;&#x67E5;&#x627E;id&#x529F;&#x80FD;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x4F20;&#x5165;<code>id</code>&#xFF0C;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE;id&#x5339;&#x914D;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<h4 id="elseif">elseif</h4>
<pre><code class="lang-js">var sql = &quot;&quot;&quot;
select * from test_data
  where 1 = 1
  &lt;if test=&quot;id == null&quot;&gt;
        and id = 0
    &lt;/if&gt;
    &lt;elseif test=&quot;id &lt; 1000&quot;&gt;
        and id = #{id}
    &lt;/elseif&gt;
&quot;&quot;&quot;
return db.select(sql)
</code></pre>
<p>&#x8FD9;&#x6761;&#x8BED;&#x53E5;&#x63D0;&#x4F9B;&#x4E86;&#x53EF;&#x9009;&#x7684;&#x67E5;&#x627E;<code>id</code>&#x529F;&#x80FD;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x4F20;&#x5165;<code>id</code>&#xFF0C;&#x5C06;&#x62FC;&#x63A5; <code>and id = 0</code>&#xFF0C;&#x5426;&#x5219;<code>id</code>&#x5C0F;&#x4E8E;<code>1000</code>&#x65F6;&#x62FC;&#x63A5;<code>and id = #{id}</code></p>
<h4 id="else">else</h4>
<pre><code class="lang-js">var sql = &quot;&quot;&quot;
select * from test_data
  where
  &lt;if test=&quot;id == null&quot;&gt;
        id = 0
    &lt;/if&gt;
    &lt;else&gt;
        id = #{id}
    &lt;/else&gt;
&quot;&quot;&quot;
return db.select(sql)
</code></pre>
<p>&#x8FD9;&#x6761;&#x8BED;&#x53E5;&#x63D0;&#x4F9B;&#x4E86;&#x53EF;&#x9009;&#x7684;&#x67E5;&#x627E;<code>id</code>&#x529F;&#x80FD;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x4F20;&#x5165;<code>id</code>&#xFF0C;&#x5C06;&#x62FC;&#x63A5; <code>and id = 0</code>&#xFF0C;&#x5426;&#x5219;&#x62FC;&#x63A5;<code>and id = #{id}</code></p>
<h4 id="where">where</h4>
<pre><code class="lang-js">var sql = &quot;&quot;&quot;
select * from test_data
&lt;where&gt;
    &lt;if test=&quot;id != null&quot;&gt;
        and id = #{id}
    &lt;/if&gt;
&lt;/where&gt;
&quot;&quot;&quot;
return db.select(sql)
</code></pre>
<p><code>where</code>&#x5143;&#x7D20;&#x53EA;&#x4F1A;&#x5728;&#x5B50;&#x5143;&#x7D20;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x5185;&#x5BB9;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x624D;&#x63D2;&#x5165;<code>WHERE</code>&#x5B50;&#x53E5;&#x3002;&#x800C;&#x4E14;&#xFF0C;&#x82E5;&#x5B50;&#x53E5;&#x7684;&#x5F00;&#x5934;&#x4E3A;<code>AND</code>&#x6216;<code>OR</code>&#xFF0C;<code>where</code>&#x5143;&#x7D20;&#x4E5F;&#x4F1A;&#x5C06;&#x5B83;&#x4EEC;&#x53BB;&#x9664;&#x3002;</p>
<p>&#x5982;&#x679C; <code>where</code>&#x5143;&#x7D20;&#x4E0E;&#x4F60;&#x671F;&#x671B;&#x7684;&#x4E0D;&#x592A;&#x4E00;&#x6837;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x81EA;&#x5B9A;&#x4E49;<code>trim</code>&#x5143;&#x7D20;&#x6765;&#x5B9A;&#x5236; <code>where</code>&#x5143;&#x7D20;&#x7684;&#x529F;&#x80FD;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x548C;<code>where</code>&#x5143;&#x7D20;&#x7B49;&#x4EF7;&#x7684;&#x81EA;&#x5B9A;&#x4E49;<code>trim</code>&#x5143;&#x7D20;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-js">&lt;trim prefix=&quot;WHERE&quot; prefixOverrides=&quot;AND |OR &quot;&gt;
  ...
&lt;/trim&gt;
</code></pre>
<p><code>prefixOverrides</code>&#x5C5E;&#x6027;&#x4F1A;&#x5FFD;&#x7565;&#x901A;&#x8FC7;&#x7BA1;&#x9053;&#x7B26;&#x5206;&#x9694;&#x7684;&#x6587;&#x672C;&#x5E8F;&#x5217;&#xFF08;&#x6CE8;&#x610F;&#x6B64;&#x4F8B;&#x4E2D;&#x7684;&#x7A7A;&#x683C;&#x662F;&#x5FC5;&#x8981;&#x7684;&#xFF09;&#x3002;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x4F1A;&#x79FB;&#x9664;&#x6240;&#x6709;<code>prefixOverrides</code>&#x5C5E;&#x6027;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x5E76;&#x4E14;&#x63D2;&#x5165;<code>prefix</code>&#x5C5E;&#x6027;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x5185;&#x5BB9;&#x3002;</p>
<h4 id="set&#x3001;trim">set&#x3001;trim</h4>
<p>&#x7528;&#x4E8E;&#x52A8;&#x6001;&#x66F4;&#x65B0;&#x8BED;&#x53E5;&#x7684;&#x7C7B;&#x4F3C;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x53EB;&#x505A;<code>set</code>&#x3002;<code>set</code>&#x5143;&#x7D20;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x52A8;&#x6001;&#x5305;&#x542B;&#x9700;&#x8981;&#x66F4;&#x65B0;&#x7684;&#x5217;&#xFF0C;&#x5FFD;&#x7565;&#x5176;&#x5B83;&#x4E0D;&#x66F4;&#x65B0;&#x7684;&#x5217;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">var sql = &quot;&quot;&quot;
update test_data
    &lt;set&gt;
        &lt;if test=&quot;name != null&quot;&gt;
            name = #{name}
        &lt;/if&gt;
        &lt;if test=&quot;content != null&quot;&gt;
            content = #{content}
        &lt;/if&gt;
    &lt;/set&gt;
    where `id` = #{id}
&quot;&quot;&quot;
return db.update(sql)
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>set</code> &#x5143;&#x7D20;&#x4F1A;&#x52A8;&#x6001;&#x5730;&#x5728;&#x884C;&#x9996;&#x63D2;&#x5165;<code>SET</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5E76;&#x4F1A;&#x5220;&#x6389;&#x989D;&#x5916;&#x7684;&#x9017;&#x53F7;&#xFF08;&#x8FD9;&#x4E9B;&#x9017;&#x53F7;&#x662F;&#x5728;&#x4F7F;&#x7528;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x7ED9;&#x5217;&#x8D4B;&#x503C;&#x65F6;&#x5F15;&#x5165;&#x7684;&#xFF09;&#x3002;</p>
<p>&#x6765;&#x770B;&#x770B;&#x4E0E;<code>set</code>&#x5143;&#x7D20;&#x7B49;&#x4EF7;&#x7684;&#x81EA;&#x5B9A;&#x4E49;<code>trim</code>&#x5143;&#x7D20;&#x5427;&#xFF1A;</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">trim</span> <span class="hljs-attr">prefix</span>=<span class="hljs-string">&quot;SET&quot;</span> <span class="hljs-attr">suffixOverrides</span>=<span class="hljs-string">&quot;,&quot;</span>&gt;</span>
  ...
<span class="hljs-tag">&lt;/<span class="hljs-name">trim</span>&gt;</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x8986;&#x76D6;&#x4E86;&#x540E;&#x7F00;&#x503C;&#x8BBE;&#x7F6E;&#xFF0C;&#x5E76;&#x4E14;&#x81EA;&#x5B9A;&#x4E49;&#x4E86;&#x524D;&#x7F00;&#x503C;&#x3002;</p>
<h4 id="foreach">foreach</h4>
<p>&#x52A8;&#x6001;<code>SQL</code>&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x662F;&#x5BF9;&#x96C6;&#x5408;&#x8FDB;&#x884C;&#x904D;&#x5386;&#xFF08;&#x5C24;&#x5176;&#x662F;&#x5728;&#x6784;&#x5EFA;<code>IN</code>&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x7684;&#x65F6;&#x5019;&#xFF09;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">var sql = &quot;&quot;&quot;
select * from test_data
where id in
&lt;foreach item=&apos;item&apos; index=&apos;index&apos; collection=&apos;body.ids&apos;
      open=&quot;(&quot; separator=&quot;,&quot; close=&quot;)&quot;&gt;
    #{item}
&lt;/foreach&gt;
&quot;&quot;&quot;
return db.select(sql)
</code></pre>
<p><code>foreach</code> &#x5143;&#x7D20;&#x7684;&#x529F;&#x80FD;&#x975E;&#x5E38;&#x5F3A;&#x5927;&#xFF0C;&#x5B83;&#x5141;&#x8BB8;&#x4F60;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#xFF0C;&#x58F0;&#x660E;&#x53EF;&#x4EE5;&#x5728;&#x5143;&#x7D20;&#x4F53;&#x5185;&#x4F7F;&#x7528;&#x7684;&#x96C6;&#x5408;&#x9879;<code>item</code>&#x548C;&#x7D22;&#x5F15;<code>index</code>&#x53D8;&#x91CF;&#x3002;&#x5B83;&#x4E5F;&#x5141;&#x8BB8;&#x4F60;&#x6307;&#x5B9A;&#x5F00;&#x5934;&#x4E0E;&#x7ED3;&#x5C3E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4EE5;&#x53CA;&#x96C6;&#x5408;&#x9879;&#x8FED;&#x4EE3;&#x4E4B;&#x95F4;&#x7684;&#x5206;&#x9694;&#x7B26;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="param.html" class="navigation navigation-prev " aria-label="Previous page: 参数获取">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="table.html" class="navigation navigation-next " aria-label="Next page: 单表操作">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"增删改查","level":"1.2.2","depth":2,"next":{"title":"单表操作","level":"1.2.3","depth":2,"path":"start/table.md","ref":"start/table.md","articles":[]},"previous":{"title":"参数获取","level":"1.2.1","depth":2,"path":"start/param.md","ref":"start/param.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"start/crud.md","mtime":"2023-12-28T06:37:08.108Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2024-01-02T08:17:25.965Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

